package com.example.myapplication2;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.example.utils.LogUtils;

public class SecondActivity extends AppCompatActivity {

    public static final String TAG = "喵喵喵";

    private EditText mEditText;
    private ImageButton mImageButton;

    private RadioGroup mRadioGroup;
    private RadioButton mRadioButton1;
    private RadioButton mRadioButton2;
    private RadioButton mRadioButton3;
    private RadioButton mRadioButton4;

    private CheckBox mCheckBox1;
    private CheckBox mCheckBox2;
    private CheckBox mCheckBox3;

    private Switch mSwitch;

    private ProgressBar mprogressBar;
    private Button mprogressButton;
    private int progress;
    private int secondProgress;
    private int max;

    private SeekBar mSeekBar;
    private TextView mSeekText;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//            Log.d("test1", "SecondActivity onCreate");

        LogUtils.d("test2", "------------SecondActivity onCreate-----------");

        System.out.println("onCreate：System.out");

        // logv： Verbose，输出普通信息
        Log.v(TAG, "onCreate: logv");
        // logd： Debug，输出调试信息
        Log.d(TAG, "onCreate: logd");
        // loge： Error，输出错误信息
        Log.e(TAG, "onCreate: loge");
        // logi： Information，输出提示信息
        Log.i(TAG, "onCreate: logi");
        // logm： Debug，输出调试信息
        Log.d(TAG, "onCreate：logm");
        // logr： Debug，输出调试信息
        Log.d(TAG, "onCreate: logr");
        // logw： Warning，输出警告信息
        Log.w(TAG, "onCreate: logw");
        // logwtf： What a terrible failure，输出非常致命的 FAULT 信息，可能杀死当前的进程
        Log.wtf(TAG, "onCreate: logwtf");


        setContentView(R.layout.activity_second);
        mEditText = findViewById(R.id.edittext);
        mImageButton = findViewById(R.id.imagebutton);
        mImageButton.setVisibility(View.GONE);
        mImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEditText.setText("");
            }
        });
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mImageButton.setVisibility(s.length() > 0 ? View.VISIBLE : View.GONE);
            }
        });

        mRadioGroup = findViewById(R.id.radiogroup);
        mRadioButton1 = findViewById(R.id.rb1);
        mRadioButton2 = findViewById(R.id.rb2);
        mRadioButton3 = findViewById(R.id.rb3);
        mRadioButton4 = findViewById(R.id.rb4);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (mRadioButton3.isChecked()) {
                    mRadioButton3.setBackground(ContextCompat.getDrawable(SecondActivity.this,R.mipmap.bg_blue));
                    mRadioButton3.setTextColor(Color.parseColor("#ff4562"));
                } else {
                    mRadioButton3.setBackground(ContextCompat.getDrawable(SecondActivity.this,R.mipmap.bg_gray));
                    mRadioButton3.setTextColor(Color.parseColor("#454562"));
                }
                if (mRadioButton1.isChecked()) {
                    mRadioButton1.setButtonDrawable(ContextCompat.getDrawable(SecondActivity.this,R.mipmap.bg_delete));
                } else {
                    mRadioButton1.setButtonDrawable(ContextCompat.getDrawable(SecondActivity.this,R.mipmap.bg_delete_pressed));
                }
            }
        });

        mCheckBox1 = findViewById(R.id.cb1);
        mCheckBox2 = findViewById(R.id.cb2);
        mCheckBox3 = findViewById(R.id.cb3);
        mCheckBox1.setChecked(true);
        mCheckBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mCheckBox1.setBackground(ContextCompat.getDrawable(SecondActivity.this,R.mipmap.bg_blue));
                } else {
                    mCheckBox1.setBackground(ContextCompat.getDrawable(SecondActivity.this,R.mipmap.bg_gray));
                }
            }
        });
        mCheckBox2.setButtonDrawable(ContextCompat.getDrawable(SecondActivity.this,R.drawable.radio_checked_selector));
        mCheckBox3.setBackgroundResource(R.drawable.radio_checked_selector);

        mSwitch = findViewById(R.id.switch1);
        mSwitch.setShowText(true);
        mSwitch.setTextOn("ON");
        mSwitch.setTextOff("OFF");
        mSwitch.setTrackDrawable(ContextCompat.getDrawable(SecondActivity.this,R.mipmap.bg_yellow));
        mSwitch.setThumbDrawable(ContextCompat.getDrawable(SecondActivity.this,R.mipmap.bg_delete_pressed));
        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                LogUtils.d("test1---1", "当前开关的状态是:" + isChecked);
            }
        });

        mprogressBar = findViewById(R.id.progress);
        progress = mprogressBar.getProgress();
        max = mprogressBar.getMax();
        secondProgress = mprogressBar.getSecondaryProgress();
        mprogressButton = findViewById(R.id.progress_btn);
        mprogressButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (progress<max){
                            mprogressBar.setProgress(++progress);
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }
                }).start();
            }
        });

        mSeekBar = findViewById(R.id.seekbar);
        mSeekText = findViewById(R.id.seektext);
        mSeekBar.setMax(120);
        mSeekBar.setThumb(getDrawable(R.mipmap.bg_delete_pressed));
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mSeekText.setText("当前进度:" + progress + "/" + mSeekBar.getMax());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                LogUtils.d("Test2","--------------开始拖动-------------");
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(SecondActivity.this,"结束拖动，进度：" +mSeekBar.getProgress()+"/"+ mSeekBar.getMax(),Toast.LENGTH_LONG).show();
            }
        });
    }
}
